//
//  ShowTableViewCell.m
//  Wetrip
//
//  Created by A5 on 14-9-16.
//  Copyright (c) 2014年 胡中磊. All rights reserved.
//

#import "BannerAdsView.h"
#define RGB_COLOR(a,b,c,d) [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:d]
@implementation BannerAdsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _imageNumber = 0;
        
        _showScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
        _showScroll.delegate = self;
        _showScroll.showsVerticalScrollIndicator = NO;
        _showScroll.showsHorizontalScrollIndicator = NO;
        _showScroll.alwaysBounceHorizontal = YES;
        _showScroll.pagingEnabled = YES;
        [self addSubview:_showScroll];
        
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.frame = CGRectMake((self.frame.size.width - 60)/2,self.frame.size.height - 20,60,10);
        _pageControl.enabled = YES;
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        //__kColorWithRGBA(0,151,224,1)
        _pageControl.currentPageIndicatorTintColor = RGB_COLOR(0, 151, 224, 1);
        [self addSubview:_pageControl];
    }
    return self;
}
- (void)function
{
    // 0 1 2 3 4 5 6
    //   0 1 2 3 4
    // 首先判断滚动到得页数
    NSInteger pageNo = _showScroll.contentOffset.x / _showScroll.frame.size.width;
    
    CGPoint contentOffset = _showScroll.contentOffset;
    contentOffset.x = (pageNo + 1) * _showScroll.frame.size.width;
    
    // 设置标题
    [UIView animateWithDuration:0.5f animations:^{
        
        _showScroll.contentOffset = contentOffset;
        // 设置分页
        //_pageControl.currentPage = pageNo == _imageNumber? 0:pageNo;
        if (pageNo == _imageNumber)
        {
            _pageControl.currentPage = 0;
        }
        else
        {
            _pageControl.currentPage=pageNo;
        }
    }];
    
    if ((pageNo + 1) == _imageNumber + 1)
    {
        // 回到第一个
        CGPoint contentOffset = _showScroll.contentOffset;
        contentOffset.x = _showScroll.frame.size.width;
        _showScroll.contentOffset = contentOffset;
    }
}
#pragma mark - 滚动视图代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 首先判断滚动到得页数
    NSInteger pageNo = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    if (pageNo == 0 || pageNo == _imageNumber + 1)
    {
        if (pageNo == 0)
        {
            pageNo = _imageNumber;
        }
        else
        {
            pageNo = 1;
        }
        scrollView.contentOffset = CGPointMake(pageNo * scrollView.frame.size.width,0);
    }
    //设置分页
    _pageControl.currentPage = pageNo - 1;
}
#pragma mark ---- 创建广告卡片
- (void)getChildViewsWithObjects:(NSArray*)images PathType:(imagePathType)imagePathType
{
    // 插入头
    UIImageView *headView = [self getImageViewWithObject:[images lastObject] PathType:imagePathType];
    headView.backgroundColor = [UIColor clearColor];
    headView.frame = CGRectMake(0,0,_showScroll.frame.size.width,_showScroll.frame.size.height);
    [_showScroll addSubview:headView];
    
    for (NSInteger i = 0;i < images.count;i ++)
    {
        UIImageView *cardView = [self getImageViewWithObject:images[i] PathType:imagePathType];
        cardView.frame = CGRectMake(self.frame.size.width * (i+1),0,_showScroll.frame.size.width,_showScroll.frame.size.height);
        [_showScroll addSubview:cardView];
    }
    
    UIImageView *footView = [self getImageViewWithObject:[images firstObject] PathType:imagePathType];
    footView.frame = CGRectMake(_showScroll.frame.size.width * (images.count+1),0,_showScroll.frame.size.width,_showScroll.frame.size.height);
    [_showScroll addSubview:footView];
    
    _imageNumber = images.count;
    // 设置滚动区域大小
    _showScroll.contentSize = CGSizeMake(_showScroll.frame.size.width * (images.count + 2),0);
    // 设置滚动的坐标
    _showScroll.contentOffset = CGPointMake(_showScroll.frame.size.width, 0);
    // 设置分页控件
    // 页数
    _pageControl.currentPage = 0;
    // 总页数
    _pageControl.numberOfPages = images.count;
    
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(function) userInfo:nil repeats:YES];
}
- (UIImageView*)getImageViewWithObject:(id)object PathType:(imagePathType)imagePathType
{
    UIImageView *cardView = [[UIImageView alloc]init];
    
    switch (imagePathType)
    {
        case imagePathTypeOne:
        {
            UIImage *image = (UIImage*)object;
            
            cardView.image = image;
        }
            break;
        case imagePathTypeTwo:
        {
            NSString *imageName = (NSString*)object;
            
            NSString *imageUrl = [NSString stringWithFormat:@"%@",imageName];
            NSURL *Url = [NSURL URLWithString:imageUrl];
            NSData *data =[NSData dataWithContentsOfURL:Url];
            [cardView setImage:[UIImage imageWithData:data]];
        }
            break;
        case imagePathTypeThree:
        {
            NSString *imageName = (NSString*)object;
            
            cardView.image = [UIImage imageNamed:imageName];
        }
            break;
        case imagePathTypeFour:
        {
            NSString *imageName = (NSString*)object;
            
            cardView.image = [UIImage imageWithContentsOfFile:imageName];
        }
            break;
        default:
            break;
    }
    
    return cardView;
}

@end
